package com.hypo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hypo.pojo.NursingLevel;
import com.hypo.pojo.NursingLevelQueryParam;
import com.hypo.pojo.PageResult;
import com.hypo.pojo.Result;
import com.hypo.service.NursingLevelService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@RestController
@RequestMapping("/nursinglevel")
@RequiredArgsConstructor
public class NursingLevelController {
    private final NursingLevelService nursingLevelService;

    /**
     * 分页查询
     *
     * @param nursingLevelQueryParam 请求参数
     * @return 返回
     */
    @GetMapping
    public Result pageQuery(NursingLevelQueryParam nursingLevelQueryParam) {
        IPage<NursingLevel> page = new Page<>(nursingLevelQueryParam.getPage(), nursingLevelQueryParam.getPageSize());
        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!Objects.isNull(nursingLevelQueryParam.getName()), NursingLevel::getName, nursingLevelQueryParam.getName())
                .eq(!Objects.isNull(nursingLevelQueryParam.getStatus()), NursingLevel::getStatus, nursingLevelQueryParam.getStatus());
        IPage<NursingLevel> pageInfo = nursingLevelService.page(page, queryWrapper);
        return Result.success(new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords()));
    }

    /**
     * 新增
     *
     * @param nursingLevel 新增数据
     * @return 返回
     */
    @PostMapping
    public Result save(@RequestBody NursingLevel nursingLevel) {
        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevelService.save(nursingLevel);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param id 删除id
     * @return 返回
     */
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        nursingLevelService.removeById(id);
        return Result.success();
    }

    /**
     * 根据id查找
     *
     * @param id id
     * @return 返回
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        NursingLevel nursingLevel = nursingLevelService.getById(id);
        return Result.success(nursingLevel);
    }

    /**
     * 修改
     *
     * @param nursingLevel 数据
     * @return 返回
     */
    @PutMapping
    public Result update(@RequestBody NursingLevel nursingLevel) {
        nursingLevelService.updateById(nursingLevel);
        return Result.success();
    }
}
